<?php $this->Html->addCrumb('Invoices', '/invoices'); ?>
<?php $this->Html->addCrumb('Invoices Awaiting Payment'); ?>
<div class="global-title">Invoices Awaiting Payment</div>
<p>&nbsp;</p>
<a style="background:#365EBA;color:#fff;font-weight:700;height:20px;padding:3px;" href="<?php echo Configure::read('base_url'); ?>invoices">Invoices Awaiting Payment</a>&nbsp;|&nbsp;
<a style="color:#000; font-weight:700; height:20px;padding:3px;" href="<?php echo Configure::read('base_url'); ?>invoices/overdue_invoices">Overdue Invoices</a>&nbsp;|&nbsp;
<a  style="color:#000; font-weight:700; height:20px;padding:3px;" href="<?php echo Configure::read('base_url'); ?>invoices/paid_invoices">Paid Invoices</a>


<?php echo $this->Form->create('Invoices', array('action' => 'index')); ?>
<br />Select Year: <select name="data[search_year]">
    <?php
    $reg_date = date("Y", strtotime($session->read('Auth.User.created')));
    $cur_date = date("Y");
    for ($i = $reg_date; $i <= $cur_date; $i++) {
        if ($i == $cur_date)
            $selected = ' selected="selected"';
        else
            $selected = '';
        ?>
        <option <?php echo $selected; ?> value="<?php echo $i; ?>"><?php echo $i; ?></option>
    <?php } ?>
</select>
<?php $months = array("01" => "January", "02" => "February", "03" => "March", "04" => "April", "05" => "May", "06" => "June", "07" => "July", "08" => "August", "09" => "September", "10" => "Octomber", "11" => "November", "12" => "December"); ?>
Select Month: <select name="data[search_month]">
    <?php
    foreach ($months as $key => $value) {
        if ($key == date('m'))
            $selected = ' selected="selected"';
        else
            $selected = '';
        echo'<option value="' . $key . '" ' . $selected . '>' . $value . '</option>';
    }
    ?>
</select>
<input type="submit" class="button" name="submit" value="Generate" />
</form>
<?php if (!empty($invoices)): ?>
    <table width="100%" border="0" cellspacing="0" cellpadding="0" class="StateTable">
        <tr class="statetablerow">
            <th style="width:150px;">Reference # / Invoice ID</th>
            <th style="width:150px;">Invoice Date</th>
            <th style="width:150px;">Invoice Due Date</th>
            <th style="width:150px;">Total Sale Amount</th>
            <th style="width:150px;">Authentic Avenue Fees</th>
            <th style="width:174px;">Pay Now</th>
        </tr>	
    </table>
    <ul class="accordinginv"  style="list-style-type: none;">
        <?php
        $i = 0;
        foreach ($invoices as $invoice):
            ?>
            <li  style="margin:0 0 3px 0">
                <table width="100%" border="0" cellspacing="0" cellpadding="0" class="StateTable">
                    <tr id="row_<?php echo $invoice['Invoice']['id']; ?>" class="invoicehead">
                        <td><?php echo $invoice['Invoice']['id']; ?></td>
                        <td><?php echo date("m-d-Y", strtotime($invoice['Invoice']['invoice_date'])); ?></td>
                        <td><?php echo date("m-d-Y", strtotime($invoice['Invoice']['invoice_due_date'])); ?></td>
                        <td>$<?php echo $invoice['Invoice']['total_sale']; ?></td>
                        <td>$<?php echo $invoice['Invoice']['aa_fee']; ?></td>
                        <td style="width:174px"><?php
        if ($invoice['Invoice']['total_sale'] > 0) {

            $invoice_date = $invoice['Invoice']['invoice_date'];
            $order_date_m1 = date("m", strtotime($invoice_date));
            $order_date_y1 = date("Y", strtotime($invoice_date));
            $order_date = date("Y-m-d H:i:s", mktime(0, 0, 0, $order_date_m1 - 1, 1, $order_date_y1));

            echo $this->Form->create('Payment', array('action' => 'expressCheckout'));
            echo $this->Form->input("invoice_id", array("type" => "hidden", "value" => $invoice['Invoice']['id']));
            ?>
                                <input type="hidden" value="AAFEE|<?php echo $invoice['Invoice']['id']; ?>" name="data[custom]">
                                <input type="image" name="submit" value="Submit" src="<?php echo Configure::read('base_url'); ?>img/paypal.gif" />
                                </form>
                                &nbsp;&nbsp;
                                <?php
                                echo $this->Form->create('Invoice', array('action' => 'process_invoice_gcheckout'));
                                echo $this->Form->input("id", array("type" => "hidden", "value" => $invoice['Invoice']['id']));
                                ?>
                                <input type="image" name="submit" value="gcheckout" src="<?php echo Configure::read('base_url'); ?>img/google_checkout.gif" />
                                </form>
        <?php } ?>

                        </td>
                    </tr>
                </table>
                <table width="100%" border="0" cellspacing="0" cellpadding="0" class="invoicetogle StateTable">	
                    <tr class="invoicehead" style="cursor:pointer;">
                        <td style="text-align:left">Month :<?php echo date("m/d/Y", strtotime($order_date)) . " - " . date("m/d/Y", strtotime($invoice_date)); ?></td>
                    </tr>
                </table>
                <ul style="list-style-type: none;<?php if ($i == 0) { ?>display:block;<?php } ?>">
                    <li>
                        <table width="100%" border="0" cellspacing="0" cellpadding="0" class="StateTable">
                            <tr>
                                <td class="nopad" colspan="6">
        <?php echo $this->requestAction("invoices/fetch_invoice_order/" . $invoice['Invoice']['id'], array('return')); ?>
                                </td>
                            </tr>
                        </table>
                    </li>
                </ul>
            </li>
            <?php
            $i++;
        endforeach;
        ?>
    </ul>
<?php else: ?>
    <br /><br />
    <h2>No awaiting payment invoices found.</h2>
    <br /><br /><br />
    <?php endif; ?>
<div class="paging">
    <?php echo $this->Paginator->prev('<< ' . __('previous', true), array(), null, array('class' => 'disabled')); ?> | 
    <?php echo $this->Paginator->numbers(); ?>
    | 
<?php echo $this->Paginator->next(__('next', true) . ' >>', array(), null, array('class' => 'disabled')); ?>
</div>
<?php //echo $this->Html->css('table_css'); ?>
<script type="text/javascript">
    jQuery('.invoicetogle').click(function(){
        var trackinv=jQuery('.invoicetogle').index(this);
        if(jQuery("ul.accordinginv li ul:eq("+trackinv+")").is(":hidden")) {
            jQuery("ul.accordinginv li ul:eq("+trackinv+")").show("slow");
        }
        else{
            jQuery("ul.accordinginv li ul:eq("+trackinv+")").hide("slow");
        }
        return false;
    });
</script>
<style type="text/css">
    table.CityTable, table.StateTable{text-align:center;width:100%;}
    table td{padding: 5px;text-align: center;vertical-align: top;width: 150px;}
    table.StateTable  th{background: #C1C1C1;padding: 5px;font-weight:700;font-size: 14px;}
    table.CityTable  th{padding: 5px; background: #C7DBF1;cursor:pointer; color:black;}	
    table.StateTable td.nopad{padding:0;vertical-align: top;}
    tr.invoicehead{padding: 5px; background: #729FDC;color:black;font-weight:700;}	
    tr.odd{background: #eee;}
    tr.headeven{padding: 5px; background-color:#EDCDCB; color:black;font-weight: 700;}
    tr.headodd{padding: 5px; background-color:#c1c1c1; color:black;font-weight: 700;}
    tr.head1odd{background:#DADADA;color:black;font-weight: 700;padding: 5px;text-decoration: underline;}
    ul.accordinginv li ul {
        display: none;  }
</style>